<?php
/**
 * Created by PhpStorm.
 * User: gyccvvh
 * Date: 2018-06-02
 * Time: 10:13
 */

namespace app\home\controller;


use thans\Ret;

use think\Controller;
use think\Request;

class File extends Controller
{
    public $ret;
    public function __construct(){
        parent::__construct();
        $this->request = Request::instance();
        $this->now_uri = $this->request->module() . '/' . $this->request->controller() . '/' . $this->request->action();
        $this->now_uri = strtolower($this->now_uri);
        $this->now_url = $this->request->path();
        $this->ret = Ret::i();
    }
    public function _form(){
        $files = input("files");
        if(!empty($files)) {
            $file_arr = explode(";", $files);
            $this->assign("files", $file_arr);
        }else{
            $this->assign("files", []);
        }
        $this->assign("filepath",$files);
        return $this->fetch();
    }

    public function upload(){
        $file = request()->file('file');
        $single = single_img(2,$file);
        if($single){
            return $this->ret->setCode(0)->setMsg($single)->toJson();
        }else{
            return $this->ret->setCode(1)->setMsg("上传失败")->toJson();
        }
    }
    public function base_upload() {
        $base64_img = urldecode(trim(input('file')));
        $up_dir = 'uploads/';
        if(!file_exists($up_dir)) {
            mkdir($up_dir,0777);
        }
        if(preg_match('/^(data:\s*image\/(\w+);base64,)/',$base64_img,$result)) {
            $type = $result[2];
            if(in_array($type,array('pjepg','jpeg','jpg','gif','bmp','png'))) {
                $saveName = $this->request->time().randomkeys(6).".".$type;
                if(file_put_contents($up_dir.$saveName,base64_decode(str_replace($result[1],'',$base64_img)))) {
                    return $this->ret->setCode(0)->setMsg($this->request->root().'/'.$up_dir.$saveName)->toJson();
                }else {
                    return $this->ret->setCode(1)->setMsg("文件上传失败")->toJson();
                }
            }else {
                return $this->ret->setCode(1)->setMsg("图片上传类型错误")->toJson();
            }
        }else {
            return $this->ret->setCode(1)->setMsg("文件错误")->toJson();
        }
    }

    public function fupload(){
        $file = request()->file('file');

        if($file){
            $filename = $file->getFilename();
            $info = $file->move(ROOT_PATH."public".DS."uploads");
            if($info){
                $filepath = $this->request->root()."/uploads".DS.$info->getSaveName();

                return $this->ret->setCode(0)->setMsg($filepath.":".$filename)->toJson();
            }else{
                return $this->ret->setCode(1)->setMsg("上传文件失败")->toJson();
            }

        }else{
            return $this->ret->setCode(1)->setMsg("上传失败")->toJson();
        }
    }

    public function _file(){
        $files = input("files");
        if(!empty($files)) {
            $file_arr = explode(";", $files);
            $this->assign("files", $file_arr);
        }else{
            $this->assign("files", []);
        }
        $this->assign("filepath",$files);
        return $this->fetch();
    }
}